#!/bin/bash
#
# $Id: install.tpl 12 2014-08-24 13:59:10Z chupin007@gmail.com $
# $HeadURL: http://bapps.googlecode.com/svn/trunk/src/enc/src/install.tpl $
#
err() {
 echo $*
  exit 1
}
if [ -z "$1" ]; then
  INSTALL=~
else
  INSTALL=$1
fi
CNGOWNER=$2
tail -n +%NUMLINES% $0 >/tmp/tmp.tar && sed -i '1d' /tmp/tmp.tar
if [ $? -ne 0 ]; then err cannot extract tar ball; fi
tar -C $INSTALL/bin -xf /tmp/tmp.tar %BINNAME%
if [ $? -ne 0 ]; then err cannot export binary; fi
if [ "$CNGOWNER" = "Y" ]; then
  sudo chown root:root $INSTALL/bin/%BINNAME%
  if [ $? -ne 0 ]; then err cannot change ownership; fi
  sudo chmod +xs $INSTALL/bin/%BINNAME%
  if [ $? -ne 0 ]; then err cannot chmod; fi
fi
if [ "%BINNAME%" = "pwenc" ] && [ ! -f $INSTALL/bin/.%BINNAME%.key ]; then
  tr -dc A-Za-z0-9 < /dev/urandom | head -c 16 >$INSTALL/bin/.%BINNAME%.key
  if [ $? -ne 0 ]; then err cannot generate key; fi
  if [ "$CNGOWNER" = "Y" ]; then
    sudo chown root:root $INSTALL/bin/.%BINNAME%.key &&
    sudo chmod 600 $INSTALL/bin/.%BINNAME%.key
    if [ $? -ne 0 ]; then err cannot set ownership on key; fi
  fi
fi
rm /tmp/tmp.tar
if [ $? -ne 0 ]; then err cannot remove temp tar ball; fi
echo %BINNAME% installed in $INSTALL/bin
exit 0
